/**
 * ===========================================
 * Project: gamehistory-web
 * ===========================================
 * Package: com.cyou.gamehistory.front.web
 * 
 * Copyright (c) 2012, CYOU All Rights Reserved.
 * ===========================================
 */
package com.cyou.gamehistory.front.web;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cyou.framework.pagination.proxy.PaginationSupport;
import com.cyou.framework.util.StringUtility;
import com.cyou.gamehistory.entity.GameHistoryFeed;
import com.cyou.gamehistory.entity.UserInfo;
import com.cyou.gamehistory.service.GameHistoryFeedService;
import com.cyou.gamehistory.util.UrlDealUtil;
import com.google.common.collect.Maps;

/**
 * <p>GameHistoryFeedController</p>
 *
 * @since 1.0
 * @version 1.0 2012-12-7
 * @author zhangxingkai
 */
@Controller
@RequestMapping(value = "/f/feed")
public class GameHistoryFeedController {

	private static final Logger logger = LoggerFactory.getLogger(GameHistoryFeedController.class);
	
	private static final int PAGE_SIZE = 8;

	private static Map<Integer, String> periods = Maps.newLinkedHashMap();
	static {
		periods.put(0 * 1000, "所有时间段");
		periods.put(60 * 1000, "最近一分钟");
		periods.put(60 * 10 * 1000, "最近十分钟");
		periods.put(60 * 30 * 1000, "最近三十分钟");
		periods.put(60 * 60 * 1000, "最近一个小时");
		periods.put(60 * 60 * 2 * 1000, "最近两个小时");
		periods.put(60 * 60 * 4 * 1000, "最近四个小时");
	}
	@Autowired
	private GameHistoryFeedService gameHistoryFeedService;

	@RequestMapping(value = "")
	public String list(Integer period, String name, Integer pageNo,
			Model model, HttpServletRequest request,HttpSession session) {

		logger.debug("feed list begin...");
		
		// 处理参数
		// 处理 period
		period = (period == null ? 0 : period);
		// 处理name
		name=this.removeSpace(name);
		
		// 处理pageNo
		pageNo = (pageNo == null || pageNo == 0 ? 1 : pageNo);

		// 查询数据
		PaginationSupport<GameHistoryFeed> feedPage = gameHistoryFeedService.findAllFeeds2(name, period, PAGE_SIZE, pageNo);

		model.addAttribute("currentPeriod", period);
		model.addAttribute("currentName", name);
		model.addAttribute("feedPage", feedPage);
		model.addAttribute("periods", periods);
		model.addAttribute("time", period);
		UserInfo hisGameHistoryCloseFlag=(UserInfo) session.getAttribute("hisGameHistoryCloseFlag");
		if(hisGameHistoryCloseFlag!=null){
			model.addAttribute("hisSpace", hisGameHistoryCloseFlag);
		}
		
		logger.debug("feed list end.");
		return "/front/feed";
	}

	@RequestMapping(value = "pull")
	@ResponseBody
	public List<GameHistoryFeed> pull() {
		List<GameHistoryFeed> feeds = gameHistoryFeedService.pull();
		return feeds;
	}
	/**
	 * 去除空格
	 * @param str
	 * @return
	 */
	public String removeSpace(String str){
		logger.info("before    str="+str);
		if(StringUtility.isNotBlank(str)){
			
			str=UrlDealUtil.decodeURIComponent(str);
			logger.info("after    str="+str);	
			str=str.replaceAll(" ", "");
			str=str.trim();
			return str;
		}
		return null;
	}

}
